#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent) , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //指明只有3个餐桌
    semaphore = new QSemaphore(3);
    //有8桌客人，每一个线程代表一桌
    for(int i = 0 ; i < 8; ++i){
     threadVector.push_back(new DinnerThread(semaphore,i+1));
    }
}

Widget::~Widget()
{
    auto it = threadVector.begin();
    for(;it != threadVector.end() ; ++it){
        (*it)->setStop();
    }

    //等待线程结束
    auto it2 = threadVector.begin();
    for(;it2 != threadVector.end() ; ++it2){
        (*it2)->wait();
    }

    qDeleteAll(threadVector.begin(),threadVector.end());
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    //允许同时读
     auto it = threadVector.begin();
     for(;it != threadVector.end() ; ++it){
         (*it)->start();
     }
}
